package com.hjm.project.team.allowanceAward.service.impl;

import java.text.ParseException;
import java.util.List;

import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.project.agentInfo.agentInfo.domain.AgentInfo;
import com.hjm.project.agentInfo.agentInfo.mapper.AgentInfoMapper;
import com.hjm.project.team.teamYlInfo.domain.TeamYlInfoDo;
import com.hjm.project.team.teamYlInfo.mapper.TeamYlInfoMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import com.hjm.project.team.allowanceAward.mapper.AllowanceAwardMapper;
import com.hjm.project.team.allowanceAward.domain.AllowanceAward;
import com.hjm.project.team.allowanceAward.service.IAllowanceAwardService;
import com.hjm.common.utils.text.Convert;

import javax.annotation.Resource;

/**
 * 创业津贴奖励配置Service业务层处理
 * 
 * @author maker-platform
 * @date 2021-04-30
 */
@Service
public class AllowanceAwardServiceImpl implements IAllowanceAwardService 
{
    @Resource
    private AllowanceAwardMapper allowanceAwardMapper;
    @Resource
    private TeamYlInfoMapper teamYlInfoMapper;   //团成员信息
    @Resource
    private AgentInfoMapper agentInfoMapper;     //代理信息

    /**
     * 查询创业津贴奖励配置
     * 
     * @param id 创业津贴奖励配置ID
     * @return 创业津贴奖励配置
     */
    @Override
    public AllowanceAward selectAllowanceAwardById(Integer id)
    {
        return allowanceAwardMapper.selectAllowanceAwardById(id);
    }

    /**
     * 查询创业津贴奖励配置列表
     * 
     * @param allowanceAward 创业津贴奖励配置
     * @return 创业津贴奖励配置
     */
    @Override
    public List<AllowanceAward> selectAllowanceAwardList(AllowanceAward allowanceAward)
    {
        return allowanceAwardMapper.selectAllowanceAwardList(allowanceAward);
    }

    /**
     * 新增创业津贴奖励配置
     * 
     * @param allowanceAward 创业津贴奖励配置
     * @return 结果
     */
    @Override
    public int insertAllowanceAward(AllowanceAward allowanceAward) throws ParseException {
        if (allowanceAward.getType() == null) {
            throw new BusinessException("请选择类型");
        }

        //查看当前展业端管理是否设置奖励,如果设置请修改不用新增
        /*AllowanceAward award = new AllowanceAward();
        award.setAgentNum(allowanceAward.getAgentNum());
        award.setType(1);
        List<AllowanceAward> allowanceAwards = allowanceAwardMapper.selectAllowanceAwardList(award);
        if (CollectionUtils.isNotEmpty(allowanceAwards)) {
            throw new BusinessException("当前展业端管理已设置团长奖励,不可重复设置");
        }
        }*/


        //如果选择的是团长
        if (allowanceAward.getType() == 1) {
            //效验是否是团长
            TeamYlInfoDo teamYlInfoDo = new TeamYlInfoDo();
            teamYlInfoDo.setAgentNum(allowanceAward.getAgentNum());
            List<TeamYlInfoDo> teamYlInfoDos = teamYlInfoMapper.selectTeamYlInfoList(teamYlInfoDo);
            //如果不存在说明不是团长
            if (CollectionUtils.isEmpty(teamYlInfoDos)) {
                throw new BusinessException("当前展业端管理不是团长,不可设置奖励");
            }
            //如果是团长查看是否被禁用
            if ("0".equals(teamYlInfoDos.get(0).getStatus())) {
                throw new BusinessException("当前团长已禁用,不可设置奖励");
            }
        }
        //如果选择的是资深总监（V9）
        if (allowanceAward.getType() == 2) {
            //效验是否是V9
            AgentInfo agentInfo = agentInfoMapper.selectAgentInfoById(allowanceAward.getAgentNum());
            if (!"0".equals(agentInfo.getAgentStatus())) {
                throw new BusinessException("当前展业端管理不是正常状态,请重新选择");
            }
            // 扶持档位 和 档位必须满足一个,当扶持档位是V9必须是不过期的
            if (!"V9".equals(agentInfo.getGrade())) {
                //查看扶持档位
                if (!"V9".equals(agentInfo.getSupportLevel())) {
                    throw new BusinessException("当前展业端管理不是资深盟主,不可设置奖励");
                }
                String supportDate = agentInfo.getSupportDate();
                //扶持日期小于当前时间
                if (DateUtils.compareDate(DateUtils.YYYY_MM_DD,supportDate,DateUtils.getDate())) {
                    throw new BusinessException("当前展业端管理资深盟主扶持已过期,不可设置奖励");
                }
            }
        }
        if(!String.valueOf(allowanceAward.getType()).equals("2")){
            allowanceAward.setTeamNum("");
            allowanceAward.setTeamName("");
            allowanceAward.setTeamAward("");
            allowanceAward.setTeamAwardType("");
        }
        allowanceAward.setAddDate(DateUtils.getDate());
        allowanceAward.setAddMan(ShiroUtils.getSysUser().getLoginName());
        return allowanceAwardMapper.insertAllowanceAward(allowanceAward);
    }

    /**
     * 修改创业津贴奖励配置
     * 
     * @param allowanceAward 创业津贴奖励配置
     * @return 结果
     */
    @Override
    public int updateAllowanceAward(AllowanceAward allowanceAward)
    {
        allowanceAward.setUpdateDate(DateUtils.getDate());
        allowanceAward.setUpdateMan(ShiroUtils.getSysUser().getLoginName());
        return allowanceAwardMapper.updateAllowanceAward(allowanceAward);
    }

    /**
     * 删除创业津贴奖励配置对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAllowanceAwardByIds(String ids)
    {
        return allowanceAwardMapper.deleteAllowanceAwardByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除创业津贴奖励配置信息
     * 
     * @param id 创业津贴奖励配置ID
     * @return 结果
     */
    @Override
    public int deleteAllowanceAwardById(Integer id)
    {
        return allowanceAwardMapper.deleteAllowanceAwardById(id);
    }
}
